package com.mikao.service.impl;


import com.mikao.config.CdnProperties;
import com.mikao.dao.HomeImageDao;
import com.mikao.dto.bo.HomePageForm;
import com.mikao.dto.bo.ReqDataTableDto;
import com.mikao.dto.vo.HomePageDto;
import com.mikao.dto.vo.RspDataTableDto;
import com.mikao.entity.Broadcast;
import com.mikao.service.HomePageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author KennyDavid
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class HomePageServiceImpl implements HomePageService {

    @Value("${cdn.baseUrl}")
    private String cdn;
    private HomeImageDao homeImageDao;
    private CdnProperties cdnProperties;

    public HomePageServiceImpl(HomeImageDao homeImageDao, CdnProperties cdnProperties) {
        this.homeImageDao = homeImageDao;
        this.cdnProperties = cdnProperties;
    }

    @Override
    public RspDataTableDto<HomePageDto> findHomePageList(ReqDataTableDto reqDataTableDto, String status) {
        List<HomePageDto> dtos = new ArrayList<>();
        HomePageDto homePageDto;
        List<Broadcast> list = homeImageDao.findAll();
        for (Broadcast homePage : list) {
            homePageDto = new HomePageDto();
            BeanUtils.copyProperties(homePage, homePageDto);
            dtos.add(homePageDto);
        }
        return new RspDataTableDto<>(reqDataTableDto.getDraw(), (long) dtos.size(), dtos);
    }

    @Override
    public void saveHomePage(HomePageForm homePageForm) {
        Broadcast homePage = new Broadcast();
        BeanUtils.copyProperties(homePageForm, homePage);

        MultipartFile file = homePageForm.getImage();
        File f = new File(cdnProperties.getBasePath());
        if (!f.exists()) {
            f.mkdirs();
        }

        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));

        File newFile = new File(f.getAbsolutePath(), newName);
        try {
            newFile.createNewFile();
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        homePage.setImageUrl(cdn + newName);
        if (ObjectUtils.isEmpty(homePageForm.getStatus())) {
            homePage.setStatus(false);
        } else {
            homePage.setStatus(true);
        }
        homePage.setType(homePageForm.getType());
        homeImageDao.saveAndFlush(homePage);
    }

    @Override
    public void updateImage(MultipartFile imageUpload, Long id) {
        File f = new File(cdnProperties.getBasePath());
        if (!f.exists()) {
            f.mkdirs();
        }
        String oldName = imageUpload.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        File newFile = new File(f.getAbsolutePath(), newName);
        try {
            newFile.createNewFile();
            imageUpload.transferTo(newFile);
            imageUpload.getInputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Broadcast homePage = homeImageDao.findOne(id);
        homePage.setImageUrl(cdn + newName);
        homeImageDao.saveAndFlush(homePage);
    }

    @Override
    public void modifyStatus(Long id) {
        Broadcast homePage = homeImageDao.findOne(id);
        homePage.setStatus(homePage.getStatus() ? false : true);
        homeImageDao.saveAndFlush(homePage);
    }
}
